<template>
  <div class="edit">
    <el-card>
      <el-tabs v-model="activeName">
        <el-tab-pane label="登陆账户设置" name="first">
          <!-- 表单区域 -->
          <el-form ref="form" :model="form" :rules="rules" label-width="100px">
            <el-form-item prop="username" label="用户名">
              <el-input v-model="form.username" />
            </el-form-item>
            <el-form-item prop="password" label="密码">
              <el-input v-model="form.password" type="password" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="submit">更新</el-button>
              <el-button>取消</el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>
        <el-tab-pane label="个人详情" name="second" lazy>
          <component
            :is="'User'"
            :user-info="newUseInfo"
            @setUserName="setUserName"
          />
        </el-tab-pane>
        <el-tab-pane
          label="岗位信息"
          name="third"
          lazy
        ><Job /></el-tab-pane>
      </el-tabs>
    </el-card>
  </div>
</template>

<script>
import { sysUserAPI, sysUserPutAPI } from '@/api'
export default {
  components: {
    User: () => import('./components/userInfo.vue'),
    Job: () => import('./components/job.vue')
  },
  data() {
    return {
      form: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '必填', trigger: 'change' },
          { min: 3, max: 12, message: '请填写3-12个字符', trigger: 'change' }
        ],
        password: [{ required: true, message: '必填', trigger: 'change' }]
      },
      activeName: 'first',
      id: this.$route.params.id,
      userInfo: {},
      newUseInfo: {}
    }
  },
  created() {
    this.getUser()
  },
  methods: {
    submit() {
      this.$refs.form.validate(async(result) => {
        if (result) {
          await sysUserPutAPI({
            ...this.userInfo,
            ...this.form
          })
          this.$message.success('更新成功')
          this.newUseInfo.username = this.from.username
        }
      })
    },
    async getUser() {
      const res = await sysUserAPI(this.id)
      this.userInfo = res.data
      this.form.username = res.data.username
      this.newUseInfo = JSON.parse(JSON.stringify(res.data))
      console.log(res.data)
    },
    setUserName(str) {
      this.form.username = str
    }
  }
}
</script>

<style lang="scss">
.edit {
  padding: 20px;
}
</style>
